const app = getApp();
var url = app.data.url;
var platFormUrl = app.data.platFormUrl;
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageType: 0,
    avatarUrl: "https://img.yzcdn.cn/vant/cat.jpeg",
    nickName: "微信昵称",
    uuid: '',
    url:''
  },
  /***
   * 退出登录
   */
  logout: function(options) {
    // 清空缓存信息
    Dialog.confirm({
      title: '提示',
      message: '是否退出登录?'
    }).then(() => {
      
      wx.setStorage({
        key: "openId",
        data: ''
      })
      wx.setStorage({
        key: "nickName",
        data: ""
      })
      wx.setStorage({
        key: "avatarUrl",
        data: ""
      })
      wx.clearStorage();
      wx.setStorage({
        key: "pageType",
        data: 0
      })
      wx.setStorage({
        key: "jwtCode",
        data: ""
      })
      let _that = this;
      _that.setData({
        pageType: 0,
        avatarUrl: "https://img.yzcdn.cn/vant/cat.jpeg",
        openId:'',
        url:''
      })
      app.globalData.openId = '';
      app.globalData.pageType = 0;
    }).catch(() => {
      // on cancel
    });

  },
  makeCall: function () {
    wx.makePhoneCall({
      phoneNumber: "18819446903",
      success: function () {
        console.log("成功拨打电话")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _that = this;
    _that.setData({
      url: ''
    })
    wx.setNavigationBarTitle({
      title: "个人中心"
    })
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getStorage({
            key: "nickName",
            success(res) {
              _that.setData({
                nickName: res.data
              })
            }
          })
          wx.getStorage({
            key: "avatarUrl",
            success(res) {
              _that.setData({
                avatarUrl: res.data
              })
            }
          })
        }
      }
    })
  },
  login: function(options) {
    let _that = this;
    let code = "";
    wx.login({
      success(res) {
        console.log(code);
        code = res.code;
      }
    })

    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        let userinfo = res.userInfo;
        //发起网络请求
        wx.request({
          url: platFormUrl + '/PfLoginApi/login',
          data: {
            nickName: userinfo.nickName,
            code: code,
            img: userinfo.avatarUrl,
            sex: userinfo.gender,
            city: userinfo.city,
            province: userinfo.province,
            country: userinfo.country,
            avatarUrl: userinfo.avatarUrl
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res);
            if (res.data.code == 200) {
              wx.showToast({
                title: "登录成功!",
                icon: "success",
                duration: 2000
              })
              console.info("登陆成功");
              console.log(res.data.data);
              wx.setStorage({
                key: "openId",
                data: res.data.data.uuid
              })
              wx.setStorage({
                key: "uuid",
                data: res.data.data.uuid
              })
              wx.setStorage({
                key: "isShowJielongDes",
                data: res.data.data.isShowJielongDes
              })
              app.globalData.openId = res.data.data.uuid
              wx.setStorage({
                key: "nickName",
                data: userinfo.nickName
              })
              wx.setStorage({
                key: "avatarUrl",
                data: userinfo.avatarUrl
              })
              wx.setStorage({
                key: "pageType",
                data: 1
              })
              wx.setStorage({
                key: "jwtCode",
                data: res.data.data.jwtToken
              })
              app.globalData.pageType = 1;
              // 设置头像 和 微信名称, pageType登录成功，设置为1
              _that.setData({
                nickName: userinfo.nickName,
                avatarUrl: userinfo.avatarUrl,
                pageType: 1,
                url: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2c045c0e5754d7d0&redirect_uri=https://am.lingdongtimes.cn/WeChatAuthApi/getServiceOpenId&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirec'
              })
            } else {
              console.log("登录失败!");
              wx.showToast({
                title: '登录失败!',
                icon: "none",
                duration: 2000
              })
            }
          },
          fail: function (res) {
            console.info('submit fail');
          },
          complete: function (res) {
            console.info('submit complete');
          }
        })
      }
    })

    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getTabBar().init();
    let _that = this;
    _that.setData({
      url: ''
    })
    wx.getStorage({
      key: "pageType",
      success(res) {
        _that.setData({
          pageType: res.data
        })
      }
    })
    wx.getStorage({
      key: "avatarUrl",
      success(res) {
        _that.setData({
          avatarUrl: res.data
        })
      }
    })
    wx.getStorage({
      key: "nickName",
      success(res) {
        _that.setData({
          nickName: res.data
        })
      }
    })
    wx.getStorage({
      key: "openId",
      success(res) {
        _that.setData({
          uuid: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})